package com.dong.zhaofengcontact2018.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dong.zhaofengcontact2018.R;
import com.dong.zhaofengcontact2018.activity.NewPersonActivity;

import java.util.ArrayList;

public class My_Editview_add_Listview_via_PopupWindow extends RelativeLayout {

    private Context ctx;
    private EditText mEditTextContent;
    private ImageView mImageViewButton;
    static final String NAMESPACE = "http://schemas.android.com/apk/res-auto";
    private boolean mIsShowArrow;
    private String mMyHint;

    private ArrayList<String> item;
    private PopupWindow mPopupWindow;


    public My_Editview_add_Listview_via_PopupWindow(Context context) {
        this(context, null);
    }

    public My_Editview_add_Listview_via_PopupWindow(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public My_Editview_add_Listview_via_PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ctx = context;


        //获取 自定义的属性值
        TypedArray typedArray = ctx.obtainStyledAttributes(attrs, R.styleable.My_Editview_add_Listview_via_PopupWindow);
        mMyHint = typedArray.getString(R.styleable.My_Editview_add_Listview_via_PopupWindow_myHint);
        mIsShowArrow = typedArray.getBoolean(R.styleable.My_Editview_add_Listview_via_PopupWindow_myIsShowArrow, true);

        initView();
        initListener();

    }


    // 按钮监听
    private void initListener() {
        mImageViewButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDropDown();
            }
        });
    }


    //获取编辑框中的内容
    public String getText() {
        return mEditTextContent.getText().toString();
    }


    private void initView() {
        RelativeLayout inflate = (RelativeLayout) View.inflate(ctx, R.layout.my_editview_add_arrow_launcher_listview, this);
        mEditTextContent = this.findViewById(R.id.item_content__editview_add_arrow_launcher_listview);
        mImageViewButton = this.findViewById(R.id.item_btn__editview_add_arrow_launcher_listview);

        mEditTextContent.setHint(mMyHint);

        if (mIsShowArrow) {
            mImageViewButton.setVisibility(VISIBLE);
        } else {
            mImageViewButton.setVisibility(GONE);
        }

    }

    public void showDropDown() {
        String tag = this.getTag().toString();
        switch (tag) {
            case "unit":
                item = ((NewPersonActivity) ctx).getUnitList();
                break;
            case "dept":
                item = ((NewPersonActivity) ctx).getDeptList();
                break;
            case "rank":
                item = ((NewPersonActivity) ctx).getRankList();
                break;
            case "office":
                item = ((NewPersonActivity) ctx).getOfficeList();
                break;
            case "roomNumber":
                item = ((NewPersonActivity) ctx).getRoomNumberList();
                break;
            case "inPhones":
                item = ((NewPersonActivity) ctx).getInPhonesList();
                break;
            case "outPhones":
                item = ((NewPersonActivity) ctx).getOutPhonesList();
                break;
            case "nickName":
                item = ((NewPersonActivity) ctx).getNickNameList();
                break;
            case "address":
                item = ((NewPersonActivity) ctx).getAddressList();
                break;
            case "inGroup":
                item = ((NewPersonActivity) ctx).getInGroupList();
                break;
        }


        ListView lv = new ListView(ctx);
        ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, item);
        lv.setAdapter(itemAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPopupWindow.dismiss();
                if (position == 0) {
                    return;
                }
                mEditTextContent.setText(item.get(position));
            }
        });

        mPopupWindow = new PopupWindow(lv, mEditTextContent.getWidth(), LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));


        mPopupWindow.showAsDropDown(mEditTextContent);
    }

}
